package com.ostrich.cloud.ostrichDistributionService.app.controller;



import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.dto.DistributionDto;
import com.ostrich.cloud.ostrichDistributionService.client.DeliveryOrderClient;
import com.ostrich.cloud.ostrichDistributionService.client.param.FinishedParam;
import com.ostrich.cloud.ostrichDistributionService.client.param.OrderParam;
import com.ostrich.cloud.ostrichDistributionService.client.param.PageSelectForm;
import com.ostrich.cloud.ostrichDistributionService.client.param.StoreParam;
import com.ostrich.cloud.ostrichDistributionService.infra.model.DeliveryOrder;
import com.ostrich.cloud.ostrichDistributionService.service.impl.DeliveryOrderServiceImpl;
import com.ostrich.cloud.param.DeliveryOrderparam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 配送订单表 前端控制器
 * </p>
 *
 * @author Tom
 * @since 2022-10-29
 */
@RestController
@Slf4j
public class DeliveryOrderController implements DeliveryOrderClient {
    @Resource
    private DeliveryOrderServiceImpl deliveryOrderService;



    @Override
    public Result storeAdd(@RequestBody StoreParam param) {
        DeliveryOrder deliveryOrder = new DeliveryOrder();
        deliveryOrder.setDeliveryId(param.getDeliveryId());
        deliveryOrder.setRiderId(param.getRiderId());
        deliveryOrder.setRiderPhoneNumber(param.getRiderPhoneNumber());
        deliveryOrder.setStatus("DISPATCHING");
        boolean b = deliveryOrderService.storeAdd(deliveryOrder);
        return b?Result.ok():Result.error();
    }

    @Override
    public Result orderAdd(@RequestBody OrderParam param) {
        DeliveryOrder deliveryOrder = new DeliveryOrder();
        deliveryOrder.setDeliveryNumber(param.getDeliveryNumber());
        deliveryOrder.setCreateTime(param.getCreateTime());
        deliveryOrder.setCustomerLocationId(param.getCustomerLocationId());
        deliveryOrder.setCustomerPhone(param.getCustomerPhone());
        deliveryOrder.setBusinessmanId(param.getBusinessmanId());
        Integer integer = deliveryOrderService.orderAdd(deliveryOrder);
        log.info("主键id:{}",integer);
        if(integer==null){
            return Result.error();
        }
        return Result.ok(integer);
    }

    @Override
    public Result getDeliveryPage(@RequestBody PageSelectForm form) {
        DistributionDto deliveryPage = deliveryOrderService.getDeliveryPage(form.getBusinessmanId(), form.getCurrentPage(), form.getSize());
        return Result.ok(deliveryPage);
    }
    
    @Override
    public Result finishedDelicery(@RequestBody FinishedParam param) {
        boolean b = deliveryOrderService.finishedDelivery(param.getDeliveryId(), param.getFinishTime());
        return b?Result.ok():Result.error();
    }

    @Override
    public DeliveryOrderparam getOne(@RequestParam("did") Integer did) {
        DeliveryOrderparam one = deliveryOrderService.getOne(did);
        return one;
    }

}

